/*
 * Copyright (C) 2014 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.camera;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import rpg.RPG;
import rpg.gfx.Bitmap;

/**
 *
 * @author Rothens
 */
public class Cinematic {

    private ArrayList<CameraMovement> movement = new ArrayList<CameraMovement>();
    private LinkedList<CinematicEffect> active = new LinkedList<CinematicEffect>();
    private LinkedList<CinematicEffect.CETimer> timer = new LinkedList<CinematicEffect.CETimer>();
    private boolean cancellable = true;
    private Camera camera = null;
    private int ticks = 0;
    private Bitmap effects = null;

    public Cinematic() {
    }
    
    public void addEffect(CinematicEffect ce, int start){
        timer.add(new CinematicEffect.CETimer(start, ce));
    }

    public void addMovement(CameraMovement cm) {
        movement.add(cm);
    }

    public void onFinish() {
        System.out.printf("[CINEMATIC] %d ticks\n", ticks);
    }

    public Bitmap getEffect() {
        return effects;
    }

    public boolean tick() {
        if (ticks == 0) {
            Collections.sort(timer);
        }
        ticks++;
        boolean ret = true;
        if (!movement.isEmpty()) {
            CameraMovement cm = movement.get(0);
            if (cm.tick()) {
                movement.remove(0);
            }
            ret = false;
        }
        if (!timer.isEmpty()) {
            Iterator<CinematicEffect.CETimer> it = timer.iterator();
            while (it.hasNext()) {
                CinematicEffect.CETimer t = it.next();
                if (t.isStarting(ticks)) {
                    active.add(t.effect);
                    it.remove();
                    Collections.sort(active);
                } else {
                    break;
                }
            }
            ret = false;
        }

        if (!active.isEmpty()) {
            effects = new Bitmap(RPG.WINDOW_WIDTH, RPG.WINDOW_HEIGHT);            
            Iterator<CinematicEffect> it = active.iterator();
            while (it.hasNext()) {
                CinematicEffect ce = it.next();
                if (ce.tick()) {
                    it.remove();
                } else {
                    effects = ce.getBitmap(effects);
                }

            }
            ret = false;
        } else {
            effects = null;
        }


        if (ret) {
            onFinish();
        }

        return ret;
    }
}
